package cn.csc.module.shop.controller.admin.slideimg;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.shop.controller.admin.slideimg.vo.*;
import cn.csc.module.shop.convert.slideimg.ShopSlideImgConvert;
import cn.csc.module.shop.dal.dataobject.slideimg.ShopSlideImgDO;
import cn.csc.module.shop.service.slideimg.ShopSlideImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 轮播图")
@RestController
@RequestMapping("/shop/slide-img")
@Validated
public class ShopSlideImgController {

    @Resource
    private ShopSlideImgService slideImgService;

    @PostMapping("/create")
    @ApiOperation("创建轮播图")
    @PreAuthorize("@ss.hasPermission('shop:slide-img:create')")
    public CommonResult<BigInteger> createSlideImg(@Valid @RequestBody ShopSlideImgCreateReqVO createReqVO) {
        return success(slideImgService.createSlideImg(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新轮播图")
    @PreAuthorize("@ss.hasPermission('shop:slide-img:update')")
    public CommonResult<Boolean> updateSlideImg(@Valid @RequestBody ShopSlideImgUpdateReqVO updateReqVO) {
        slideImgService.updateSlideImg(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除轮播图")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:slide-img:delete')")
    public CommonResult<Boolean> deleteSlideImg(@RequestParam("id") BigInteger id) {
        slideImgService.deleteSlideImg(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得轮播图")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:slide-img:query')")
    public CommonResult<ShopSlideImgRespVO> getSlideImg(@RequestParam("id") BigInteger id) {
        ShopSlideImgDO slideImg = slideImgService.getSlideImg(id);
        return success(ShopSlideImgConvert.INSTANCE.convert(slideImg));
    }

    @GetMapping("/list")
    @ApiOperation("获得轮播图列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('shop:slide-img:query')")
    public CommonResult<List<ShopSlideImgRespVO>> getSlideImgList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<ShopSlideImgDO> list = slideImgService.getSlideImgList(ids);
        return success(ShopSlideImgConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得轮播图分页")
    @PreAuthorize("@ss.hasPermission('shop:slide-img:query')")
    public CommonResult<PageResult<ShopSlideImgRespVO>> getSlideImgPage(@Valid ShopSlideImgPageReqVO pageVO) {
        PageResult<ShopSlideImgDO> pageResult = slideImgService.getSlideImgPage(pageVO);
        return success(ShopSlideImgConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出轮播图 Excel")
    @PreAuthorize("@ss.hasPermission('shop:slide-img:export')")
    @OperateLog(type = EXPORT)
    public void exportSlideImgExcel(@Valid ShopSlideImgExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopSlideImgDO> list = slideImgService.getSlideImgList(exportReqVO);
        // 导出 Excel
        List<ShopSlideImgExcelVO> datas = ShopSlideImgConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "轮播图.xls", "数据", ShopSlideImgExcelVO.class, datas);
    }

}
